package cn.yeyiu.netonlinevideo.parser.video;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.bean.TypeData;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.MediaData;
import cn.yeyiu.util.bean.VideoInfo;

public class CMParser extends Parser {

    Map<String, String> typeMap = new HashMap<>();

    public CMParser(SourceData source) {
        super(source);
    }

    @Override
    public List<TypeData> getTypeList() {
        String apiurl = source.url + "videosort";
        String jsonStr = NetUtil.getByUrl(apiurl);
        JSONArray array = JSONObject.parseObject(jsonStr).getJSONArray("rescont");

        List<TypeData> types = new ArrayList<>();
        for (int i = 0; i < array.size(); ++i) {
            JSONObject object = array.getJSONObject(i);
            String categoryId = object.getString("id");
            String type = object.getString("name");
            types.add(new TypeData(type));
            typeMap.put(type, categoryId);
        }
        return types;
    }

    @Override
    public Object getBaseData(String type, int pageIndex) {
        String categoryId = typeMap.get(type);
        String url = source.url + String.format(Locale.CHINA, "videosort/%s?orderby=hot&page=%d", categoryId, pageIndex + 1);
        String jsonStr = NetUtil.getByUrl(url);
        return JSONObject.parseObject(jsonStr);
    }

    @Override
    public List<MediaData> getDataList(Object data, String type) {
        JSONObject jo = (JSONObject) data;
        JSONArray jsonArray = jo.getJSONObject("rescont").getJSONArray("data");

        List<MediaData> videoInfos = new ArrayList<>();
        MediaData mediaData;
        VideoInfo videoInfo;
        for (int i = 0; i < jsonArray.size(); ++i) {
            videoInfo = new VideoInfo();
            mediaData = new MediaData();

            JSONObject object = jsonArray.getJSONObject(i);
            String id = object.getString("id");
            videoInfo.code = source.code;
            videoInfo.type = type;
            videoInfo.title = object.getString("title");
            getPicContent(videoInfo, object.getJSONObject("coverbase64").getString("url"));

            mediaData.id = id;
            mediaData.wrap(videoInfo);
            videoInfos.add(mediaData);
        }
        return videoInfos;
    }

    void getPicContent(VideoInfo videoInfo, String pic_url) {
        String base64Pic = NetUtil.getByUrl(pic_url);
        base64Pic = base64Pic.substring(base64Pic.indexOf("/9j/"));
        videoInfo.picSrc = base64Pic;
    }

    @Override
    public Object buildData(MediaData mediaData) {
        VideoInfo videoInfo = (VideoInfo) mediaData.data;
        String jsonStr = NetUtil.getByUrl(source.url + String.format("videoplay/%s", mediaData.id));
        videoInfo.videoSrc = JSONObject.parseObject(jsonStr).getJSONObject("rescont").getString("videopath");
//        if ("".equals(url)) {
//            initUser();
//            html = NetUtil.postByUrl(source.url + "movie/play", data, false, header);
//            json = getJson(html);
//            url = json.getJSONObject("data").getString("url");
//        }
        return videoInfo;
    }
}
